package com.dycx.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.context.UserInfoContext;
import com.dycx.system.service.RegionService;
import com.dycx.system.service.OssFileService;
import com.dycx.user.converter.SellerConverter;
import com.dycx.user.dto.SellerDTO;
import com.dycx.user.entity.SellerDO;
import com.dycx.user.entity.SellerRejectLogDO;
import com.dycx.user.enums.SellerStatusEnum;
import com.dycx.user.mapper.SellerMapper;
import com.dycx.user.service.IndustryTypeService;
import com.dycx.user.service.SellerRejectLogService;
import com.dycx.user.service.SellerService;
import com.dycx.user.service.UserAuthService;
import com.dycx.user.vo.SellerVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
@AllArgsConstructor
public class SellerImpl extends ServiceImpl<SellerMapper, SellerDO> implements SellerService {

    private SellerRejectLogService sellerRejectLogService;
    private UserAuthService userAuthService;
    private OssFileService ossFileService;
    private SellerMapper sellerMapper;
    private RegionService regionService;
    private IndustryTypeService industryTypeService;

    /**
     * 删除管理员
     *
     * @param id
     * @return
     */
    @Override
    public Boolean delete(int id) {
        SellerDO sellerDO = getById(id);
        if (removeById(id)) return false;

        ossFileService.setDeleted(sellerDO.getAvatar());
        ossFileService.setDeleted(sellerDO.getCompanyBusinessLicense());
        ossFileService.setDeleted(sellerDO.getBanner());

        return true;
    }

    /**
     * 添加商家
     *
     * @param sellerDto
     * @return
     */
    @Override
    public Boolean addSeller(SellerDTO sellerDto) {
        LambdaQueryWrapper<SellerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SellerDO::getUid, sellerDto.getUid());
        if (ObjectUtil.isNotEmpty(getOne(wrapper))) {
            throw new RuntimeException("当前用户已存在商家信息，请勿重复添加");
        }

        SellerDO sellerDO = regionService.replenish(BeanUtil.toBean(sellerDto, SellerDO.class));
        sellerDO.setCheckStatus(SellerStatusEnum.SELLER_CHECK_STATUS_SUCCESS.getCode());

        if (!save(sellerDO)) {
            throw new RuntimeException("提交失败，请检查提交内容，或联系平台处理");
        }

        ossFileService.setUse(sellerDto.getAvatar());
        ossFileService.setUse(sellerDto.getCompanyBusinessLicense());
        ossFileService.setUse(sellerDto.getBanner());

        //设置日志
        sellerDto.setId(sellerDO.getId());

        return true;
    }

    /**
     * 修改商家
     *
     * @param sellerDto
     * @return
     */
    @Override
    public Boolean edit(SellerDTO sellerDto) {
        SellerDO originSellerDO = getById(sellerDto.getId());

        LambdaQueryWrapper<SellerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SellerDO::getUid, sellerDto.getUid());
        wrapper.ne(SellerDO::getId, sellerDto.getId());
        if (ObjectUtil.isNotNull(getOne(wrapper))) {
            throw new RuntimeException("uid已存在商家信息，请勿重复添加");
        }

        SellerDO sellerDO = regionService.replenish(BeanUtil.toBean(sellerDto, SellerDO.class));

        if (!updateById(sellerDO)) {
            throw new RuntimeException("提交失败，请检查提交内容，或联系平台处理");
        }

        ossFileService.setDeleted(originSellerDO.getAvatar());
        ossFileService.setDeleted(originSellerDO.getCompanyBusinessLicense());
        ossFileService.setDeleted(originSellerDO.getBanner());

        ossFileService.setUse(sellerDto.getAvatar());
        ossFileService.setUse(sellerDto.getCompanyBusinessLicense());
        ossFileService.setUse(sellerDto.getBanner());

        //设置日志
        sellerDto.setId(sellerDO.getId());

        return true;
    }


    /**
     * 新增审核和被退回重新审核
     *
     * @param sellerDto
     * @return
     */
    @Override
    public Boolean sellerApply(SellerDTO sellerDto) {
        SellerDO originSellerDO = getById(sellerDto.getId());

        SellerDO sellerDO = BeanUtil.toBean(sellerDto, SellerDO.class);
        sellerDO.setUid(UserInfoContext.get().getUserId());
        sellerDO.setCheckStatus(SellerStatusEnum.SELLER_CHECK_STATUS_ING.getCode());
        sellerDO.setCompanyBusinessLicense(sellerDto.getCompanyBusinessLicense());
        sellerDO.setCreateTime(LocalDateTime.now());
        if (!save(sellerDO)) {
            throw new RuntimeException("提交失败，请检查提交内容，或联系平台处理");
        }

        ossFileService.setDeleted(originSellerDO.getCompanyBusinessLicense());
        ossFileService.setUse(sellerDto.getCompanyBusinessLicense());

        return true;
    }

    /**
     * 审核商家
     *
     * @param sellerDto
     * @return
     */
    @Override
    public Boolean checkSeller(SellerDTO sellerDto) {

        SellerDO sellerDO = new SellerDO();
        sellerDO.setId(sellerDto.getId());
        sellerDO.setCheckStatus(sellerDto.getCheckStatus());
        if (ObjectUtil.isEmpty(save(sellerDO))) {
            throw new RuntimeException("修改商家状态失败");
        }

        if (sellerDto.getCheckStatus().equals(SellerStatusEnum.SELLER_CHECK_STATUS_REJECT.getCode())) { //拒绝
            SellerRejectLogDO sellerRejectLogDO = new SellerRejectLogDO();
            sellerRejectLogDO.setSellerId(sellerDto.getId());
            sellerRejectLogDO.setRemark(sellerDto.getCheckRemark());

            sellerRejectLogService.save(sellerRejectLogDO);
        }

        return true;
    }

    @Override
    public SellerVO getInfoById(Integer id) {
        SellerVO sellerVO = BeanUtil.toBean(getById(id), SellerVO.class);

        sellerVO.setIndustryTypeIdLevel(industryTypeService.getIndustryTypeIdLevel(sellerVO.getIndustryType()));

        return sellerVO;
    }

    @Override
    public SellerVO getSellerInfoByUid(Integer uid) {
        LambdaQueryWrapper<SellerDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SellerDO::getUid, uid);
        SellerDO sellerDO = sellerMapper.selectOne(queryWrapper);

        if (ObjectUtil.isNull(sellerDO)) {
            return null;
        }

        SellerVO sellerVo = BeanUtil.toBean(sellerDO, SellerVO.class);

        if (sellerVo.getCheckStatus().equals(SellerStatusEnum.SELLER_CHECK_STATUS_REJECT.getCode())) {
            LambdaQueryWrapper<SellerRejectLogDO> queryLogWrapper = new LambdaQueryWrapper<>();
            queryLogWrapper.eq(SellerRejectLogDO::getSellerId, sellerVo.getId());
            queryLogWrapper.orderByDesc(SellerRejectLogDO::getId);
            queryLogWrapper.last(" limit 1 ");

            SellerRejectLogDO sellerRejectLogDO = sellerRejectLogService.getOne(queryLogWrapper);
            sellerVo.setRejectReason(sellerRejectLogDO.getRemark());
        }

        return sellerVo;
    }

    @Override
    public Page<SellerVO> getPage(SellerDTO sellerDto) {
        LambdaQueryWrapper<SellerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(sellerDto.getUid()), SellerDO::getUid, sellerDto.getUid());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getCompanyName()), SellerDO::getCompanyName, sellerDto.getCompanyName());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getCompanyCode()), SellerDO::getCompanyCode, sellerDto.getCompanyCode());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getCompanyLegalPerson()), SellerDO::getCompanyLegalPerson, sellerDto.getCompanyLegalPerson());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getCompanyLegalPersonIdCard()), SellerDO::getCompanyLegalPersonIdCard, sellerDto.getCompanyLegalPersonIdCard());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getCompanyLegalPersonMobile()), SellerDO::getCompanyLegalPersonMobile, sellerDto.getCompanyLegalPersonMobile());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getContactName()), SellerDO::getContactName, sellerDto.getContactName());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getContactIdCard()), SellerDO::getContactIdCard, sellerDto.getContactIdCard());
        wrapper.eq(StrUtil.isNotBlank(sellerDto.getContactMobile()), SellerDO::getContactMobile, sellerDto.getContactMobile());
        wrapper.eq(ObjectUtil.isNotNull(sellerDto.getProvinceCode()), SellerDO::getProvinceCode, sellerDto.getProvinceCode());
        wrapper.eq(ObjectUtil.isNotNull(sellerDto.getCityCode()), SellerDO::getCityCode, sellerDto.getCityCode());
        wrapper.eq(ObjectUtil.isNotNull(sellerDto.getDistrictCode()), SellerDO::getDistrictCode, sellerDto.getDistrictCode());
        wrapper.eq(ObjectUtil.isNotNull(sellerDto.getStreetCode()), SellerDO::getStreetCode, sellerDto.getStreetCode());
        wrapper.eq(ObjectUtil.isNotNull(sellerDto.getCheckStatus()), SellerDO::getCheckStatus, sellerDto.getCheckStatus());

        Page<SellerDO> entityPage = page(new Page<>(sellerDto.getPageIndex(), sellerDto.getPageSize()), wrapper);

        return SellerConverter.entityToVoPage(entityPage);
    }

    @Override
    public String lookSellerIdCard(int sellerId) {
        SellerDO sellerDO = sellerMapper.selectById(sellerId);
        return sellerDO.getContactIdCard();
    }
}
